////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.data {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	
	import kiwi.utils.Validation;
	
	import plantool.core.CoreDispatcher;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="connection_ready", type="plantool.data.DatabaseManager")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class DatabaseManager extends CoreDispatcher {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		public static const CONNECTION_READY:String			= "connection_ready";
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private static var _instance:DatabaseManager		= null;
		private var _controller:IDatabaseController			= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function DatabaseManager(pvt:PrivateConstructorClass) {
			super();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		public static function getInstance():DatabaseManager {
			//check if instance exists, if not create one
			if (!Validation.isSet(_instance)) {
				_instance = new DatabaseManager(new PrivateConstructorClass());
			}
			
			//return instance
			return _instance;
		}
		
		private function onController_connected(e:Event):void {
			//remove event
			_controller.removeEventListener(DatabaseController.CONNECTED		, onController_connected);
			
			//set application status
			statusManager.status	= "Database opened...";
			
			//inform listeners
			dispatchEvent(new Event(CONNECTION_READY));
		}
		
		public function close():void {
			_controller.close();
		}
		
		public function connect():void {
			_controller.connect();
		}
		
		public function setController(dbaseController:IDatabaseController):void {
			//set application status
			statusManager.status	= "Opening database...";
			
			//set a controller to handle the database connection / queries
			_controller		= dbaseController;

			//connect the controller
			_controller.addEventListener(DatabaseController.CONNECTED		, onController_connected);
			_controller.connect();
		}
		
		public function createDateFromTimestamp(timestamp:Number):Date {
			return _controller.createDateFromTimestamp(timestamp);
		}
		
		public function createTable(table:Table, onCompleteHandler:Function):void {
			_controller.createTable(table, onCompleteHandler);
		}
		
		public function formatDate(date:Date):String {
			return _controller.formatDate(date);
		}
		
		public function formatDateField(field:String):String {
			return _controller.formatDateField(field);
		}
		
		public function getFieldName(type:uint):String {
			return _controller.getFieldName(type);
		}
		
		public function query(q:String, onCompleteHandler:Function, onErrorHandler:Function, itemClass:Class = null, userdata:* = null):void {
			//pass the query on to the database controller
			_controller.query(q, onCompleteHandler, onErrorHandler, itemClass, userdata);
		}
		
		public function transaction(q:Array, onCompleteHandler:Function, onErrorHandler:Function, userdata:* = null):void {
			//pass the query on to the database controller
			_controller.transaction(q, onCompleteHandler, onErrorHandler, userdata);
		}
	}
}

//--------------------------------------------------------------------------
//END OF PACKAGE
//Define the PrivateConstructorClass here so an instance cannot be by anyone
//except for the class defined in the above package
//--------------------------------------------------------------------------
class PrivateConstructorClass {
	//--------------------------------------------------------------------------
	//Constructor and Destructor
	//--------------------------------------------------------------------------
	public function PrivateConstructorClass() {
	}
}